package org.example.demoblg.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.example.demoblg.model.Blog;
import org.example.demoblg.model.User;
import org.example.demoblg.result.Constant;
import org.example.demoblg.result.Jwt;
import org.example.demoblg.result.Result;
import org.example.demoblg.serviser.ServicerBlog;

import org.example.demoblg.serviser.ServicerUse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
@RestController
@RequestMapping("blog")
@ResponseBody
public class ControllerBlog {
    private static final Logger log = LoggerFactory.getLogger(ControllerBlog.class);
    @Autowired
    private ServicerUse servicerUse;
    @Autowired
    private ServicerBlog servicerBlog;
    @RequestMapping("/getbloglist")
    public List<Blog> getAll(){
        return  servicerBlog.getBlogList();
    }
    public Blog getBlogById(int id){
        return servicerBlog.getBlog(id);
    }
    @RequestMapping("/add")
    public Result addBlog(HttpServletRequest request,String title,String content) {
        String token=request.getHeader("user_token");
        log.info("token:"+token);
        Integer userId= Jwt.idbytoken(token);
        Blog blog=new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(userId);
        if (blog.getUserId() <= 0) {
            return Result.fail(Constant.RESULT_CODE_FAIL, "添加失败");
        }
        Integer result = servicerBlog.addBlog(blog);
  if(result<1){

      return Result.fail(Constant.RESULT_CODE_FAIL, "添加失败");
    }
        return Result.success("添加成功");
    }

    @RequestMapping("/update")
    public Result updateBlog(Blog blog){


        if(blog.getId() <= 0){
            return Result.fail(Constant.RESULT_CODE_FAIL,"更新失败");
        }
        int result= servicerBlog.updateBlog(blog);
        if(result<1){
            return Result.fail(Constant.RESULT_CODE_FAIL,"更新失败");
        }
       return  Result.success("true");
    }
    @RequestMapping("/delete")
    public Result deleteBlog(int blogId){
        if(blogId <= 0){
            return Result.fail(Constant.RESULT_CODE_FAIL,"删除失败");
        }

        int result= servicerBlog.deleteBlog(blogId);
        if(result<1){
            return Result.fail(Constant.RESULT_CODE_FAIL,"删除失败");
        }

        return  Result.success("true");
    }
    @RequestMapping("/getBlogByid")
    public Result getBlogById(Integer blogId){
        log.info("getBlogById",+blogId);
        if(blogId <= 0){
            return  Result.fail(Constant.RESULT_CODE_FAIL,"错误");
        }
         Blog blog= servicerBlog.getBlog(blogId);
        return Result.success(blog);
    }
    @RequestMapping("/isauthor")
    public Result iszuthor(HttpServletRequest request,Integer blogId){
        String token=request.getHeader("user_token");
        log.info("token:"+token);
        Integer userId= Jwt.idbytoken(token);
        Blog blog=servicerBlog.getBlog(blogId);
        if(blog.getUserId()!=userId){
            return  Result.fail(Constant.RESULT_CODE_FAIL,"博客不是当前登录的用户的");
        }
        return Result.success(blog);

    }
    @RequestMapping("/getNameByBlogId")
    public Result getNameByBlogId(Integer blogId){
        log.info("getNameByBlogId",+blogId);
        if(blogId <= 0){
            return null;
        }
        Blog blog= servicerBlog.getBlog(blogId);
        Integer userId = blog.getUserId();
        if(userId <= 0){
            return null;
        }
        User user=servicerUse.getNameById(userId);
       return Result.success(user);

    }
}
